package com.atguigu.msmservice.service.Impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msmservice.service.MsmService;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

/**
 * @author He Wentao
 * @since 2022/9/16 22:32
 */

@Service
public class MsmServiceImpl implements MsmService {


    @Override
    public Boolean send(String fourBitRandom, String phone) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI5tKchScgi7GDrZZR", "0kychgy5mixj03aYIJUzqOTfxjj");

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
        request.setPhoneNumbers(phone);
        request.setTemplateParam("{\"code\":\""+fourBitRandom+"\"}");
        System.out.println("{\"code\":\""+fourBitRandom+"\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
            return false;
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            return false;
        }

        return true;
    }
}
